//
//  UserListViewController.m
//  UI_豆瓣扩展
//
//  Created by dllo on 15/11/8.
//  Copyright © 2015年 张磊. All rights reserved.
//

#import "UserListViewController.h"
#import "UserTableViewCell.h"
#import "FirstViewController.h"
#import "SDImageCache.h"
#import "MBProgressHUD.h"
#import "MyMovieViewController.h"
#import "MyActivityViewController.h"
@interface UserListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, retain)UITableView *myTableView;
@property (nonatomic, retain)UIButton *button;
@property (nonatomic, assign)BOOL isSelected;

@end

@implementation UserListViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSString *isLogin = [[NSUserDefaults standardUserDefaults]stringForKey:@"isLogin"];
    if ([isLogin isEqualToString:@"1"]) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"注销" style:UIBarButtonItemStylePlain target:self action:@selector(rightBar:)];
    }else{
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(rightBar:)];
    }
}

- (void)rightBar:(UIBarButtonItem *)btn
{
    FirstViewController *firstVC = [[FirstViewController alloc]init];
    if ([[NSUserDefaults standardUserDefaults]stringForKey:@"isLogin"] != nil) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"是否注销" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *canact = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            NSLog(@"取消");
        }];
        UIAlertAction *othact = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSLog(@"OK");
            
            [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"isLogin"];
            
            UIAlertController *alertOut = [UIAlertController alertControllerWithTitle:@"注销" message:@"注销成功" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *canactOut = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                NSLog(@"取消");
                
                UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:firstVC];
                [self.navigationController presentViewController:navC animated:YES completion:nil];
            }];
            [alertOut addAction:canactOut];
            [self presentViewController:alertOut animated:YES completion:nil];
        }];
        [alert addAction:canact];
        [alert addAction:othact];
        [self presentViewController:alert animated:YES completion:nil];
        
    }else{
        //点击登陆
        UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:firstVC];
        [self.navigationController presentViewController:navC animated:YES completion:nil];
    }
}



- (void)dealloc
{
    [_myTableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    
    self.navigationItem.title = @"我的";
    self.view.backgroundColor = [UIColor whiteColor];
    self.myTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [_myTableView release];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(RightBar:)];
    
    
    //注册cell
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    // 去掉多余的分割线
    self.myTableView.tableFooterView = [UIView new];
    //去掉cell分割线
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
}

-(void)RightBar:(UIBarButtonItem *)barBtn
{
        FirstViewController *firstVC = [[FirstViewController alloc]init];
        [self presentViewController:firstVC animated:YES completion:nil];
        [firstVC release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的活动";
    }else if (indexPath.row == 1){
        cell.textLabel.text = @"我的电影";
    }else{
        cell.textLabel.text = @"清除缓存";
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"登陆"]) {
        if (indexPath.row == 0 || indexPath.row == 1) {
            FirstViewController *firstVC = [[FirstViewController alloc]init];
            UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:firstVC];
            [self presentViewController:navC animated:YES completion:^{
            }];
            [firstVC release];
            
        }else{
            //获取SDImageCache缓存图片大小
            NSUInteger size = [[SDImageCache sharedImageCache]getSize];
            float cacheSize = size / 1024.0 / 1024.0;
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"清理缓存" message:@"是否确认注销" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alertC animated:YES completion:nil];
            
            UIAlertAction *alertC1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alertC addAction:alertC1];
            UIAlertAction *alertC2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
                [[SDImageCache sharedImageCache]clearDisk];
                NSLog(@"缓存大小:%0.2f MB",cacheSize);
                MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                hud.mode = MBProgressHUDModeText;
                hud.labelText = @"缓存清理成功";
                hud.margin = 10.f;
                hud.removeFromSuperViewOnHide = YES;
                [hud hide:YES afterDelay:1];
            }];
            [alertC addAction:alertC2];
        }
        
    }else{
        if (indexPath.row == 0){
            MyActivityViewController *myActivityVC = [[MyActivityViewController alloc]init];
            
            [self.navigationController pushViewController:myActivityVC animated:YES];
            [myActivityVC release];
        }else if (indexPath.row == 1){
            MyMovieViewController *myMovieVC = [[MyMovieViewController alloc]init];
            [self.navigationController pushViewController:myMovieVC animated:YES];
            [myMovieVC release];
        }
    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
